/************************************************************
* author: alexander.aivars <alexander.aivars&#64;svt.se>
* $Date:2007-12-19 11:39:12 +0100 (Wed, 19 Dec 2007) $
* $Revision:196 $
* $HeadURL:svn://stoiis03.internt.svt.se/SVTiFLASH/trunk/SVTSE/Source/Classes/se/svt/web/net/TipsaKompis.as $
* $Id:TipsaKompis.as 196 2007-12-19 10:39:12Z alai $
************************************************************/
package se.svt.web.net
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLVariables;
	
	import se.svt.web.events.NetDataRequestEvent;
	import se.svt.web.models.NetData;
	import se.svt.web.models.TipsaKompisData;



	/**
	* Dispatched when data has finished loading.
	*
	* @eventType se.svt.web.events.NetDataRequestEvent.COMPLETE
	*/
	[Event(name="NetDataRequstComplete", type="se.svt.web.events.NetDataRequestEvent")]

	/**
	* Dispatched when data has loaded successfully.
	*
	* @eventType se.svt.web.events.NetDataRequestEvent.
	*/
	[Event(name="NetDataRequstSuccess", type="se.svt.web.events.NetDataRequestEvent")]

	/**
	* Dispatched when data has loaded with error response.
	*
	* @eventType se.svt.web.events.NetDataRequestEvent.
	*/
	[Event(name="NetDataRequstFailed", type="se.svt.web.events.NetDataRequestEvent")]

	/**
	* Dispatched when an error occurs that causes a load operation to fail.
	*
	* @eventType se.svt.web.events.NetDataRequestEvent.
	*/
	[Event(name="NetDataRequstError", type="se.svt.web.events.NetDataRequestEvent")]
	
    /**
     *	Create a TipsaKompis article object
     *	@author Alexander Aivars
     *  @example 
     *	<listing version="3.0" >
	 *	package {
	 * 
	 *	    import flash.display.Sprite;
	 *	    import flash.events.Event;
	 *	    import se.svt.web.events.NetDataRequestEvent;
	 * 
	 *	    public class TipsaKompisExample extends Sprite
	 *	    {
	 *	       public function TipsaKompisExample() {
	 *	           var tipsaKompisArticle:TipsaKompis = new TipsaKompis(698602);
	 *	           var variables:TipsaKompisData = new TipsaKompisData();
	 *	        
	 *	           variables.fromemail = "my.email&#64;domain.com";
	 *	           variables.fromname = "My Name";
	 *	           variables.toemail = "john.doe&#64;domain.com";
	 *	           variables.toname = "John Doe";
	 *	           variables.urlinmail = "http://svt.se";
	 *	        
	 *	           tipsaKompisArticle.addEventListener(NetDataRequestEvent.FAILED,onSendFailed);
	 *	           tipsaKompisArticle.addEventListener(NetDataRequestEvent.SUCCESS,onSendSuccess);
	 *	   
	 *	           tipsaKompisArticle.post(variables);
	 *	       }
	 *	
	 *	       private function onSendFailed(event:NetDataRequestEvent):void {
	 *	           trace("Send failed.");
	 *	           trace("server response:\n" + event.data.responseXml );
	 *	       }
	 *	
	 *	       private function onSendSuccess(event:NetDataRequestEvent):void {
	 *	           trace("Send successful.");
	 *	           trace("server response:\n" + event.data.responseXml );
	 *	       }
	 *	    }
	 * 
	 *	}
     *	</listing>
     */
    	public class TipsaKompis extends EventDispatcher {

		private static const TIPSAKOMPIS_URL:String = "http://svt.se/svt/road/Classic/shared/svttools/Flashtipsakompis.jsp";
		private var _articelId:int;
		private var _data:NetData;
		
		/**
		 * Creates a new TipsaKompis instance.
		 * @param articelId Article Id of published Polopoly article <i>Tipsa kompis (703)</i> 
		 * 
		 */
		public function TipsaKompis(articelId:int)
		{
			_articelId = articelId;
		}


        /**
         * 
         * @param data Email content variables
         * 
         */
        public function post(data:TipsaKompisData):void {
			
			var loader:URLLoader = new URLLoader();
            var request:URLRequest = new URLRequest(TIPSAKOMPIS_URL);
            var variables:URLVariables = new URLVariables();

			_data = data;
			variables.tipsArtid = _articelId;

			if(data.toemail!=null) variables.toemail = data.toemail;
			if(data.toname!=null) variables.toname = data.toname;
			if(data.toemail!=null) variables.toemail = data.toemail;
			if(data.fromname!=null) variables.fromname = data.fromname;	
			if(data.fromemail!=null) variables.fromemail = data.fromemail;	
			if(data.urlinmail!=null) variables.urlinmail = data.urlinmail;
			if(data.param0!=null) variables.param0 = data.param0;
			if(data.param1!=null) variables.param1 = data.param1;
			if(data.param2!=null) variables.param2 = data.param2;
			if(data.param3!=null) variables.param3 = data.param3;
			if(data.param4!=null) variables.param4 = data.param4;

			request.data = variables;
			addPostListners(loader);
            
            try {
            	try {
	                loader.load(request);
            	} catch (error:SecurityError) { trace("SecurityError send form."); }
            } catch (error:Error) {
                trace(error.name +" : Unable to load requested document.");
            }
			
		}
				
		private function addPostListners(dispatcher:IEventDispatcher):void {
            dispatcher.addEventListener(Event.COMPLETE, postComplete);
            dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
        }
		
		private function removePostListners(dispatcher:IEventDispatcher):void {
            dispatcher.removeEventListener(Event.COMPLETE, postComplete);
            dispatcher.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
        }

		private function ioErrorHandler(event:IOErrorEvent):void {
            dispatchEvent(new NetDataRequestEvent(NetDataRequestEvent.ERROR));
        }
                        
		private function postComplete(event:Event):void {
            var loader:URLLoader = event.target as URLLoader;
			//var response:Namespace  = new Namespace("http://svt.se/xmlnamespaces/svtFlash/externalDataResponse/");

			var xml:XML = new XML(loader.data);

			_data.responseXml = xml;
	
			if (xml..message.@isError.toString().toLowerCase() == "false") {
				dispatchEvent(new NetDataRequestEvent(NetDataRequestEvent.SUCCESS,_data));

			} else {
				dispatchEvent(new NetDataRequestEvent(NetDataRequestEvent.FAILED,_data));
			}

			dispatchEvent(new NetDataRequestEvent(NetDataRequestEvent.COMPLETE,_data));

            removePostListners(loader);
        }
        

	}
}
